package com.smile.frame.mq.rocket.controller;


import com.smile.frame.common.result.Result;
import com.smile.frame.mq.rocket.config.RocketMqProducer;
import com.smile.frame.mq.rocket.constants.cart.CartTopic;
import com.smile.frame.mq.rocket.message.CartItemEvent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author LGC
 * @date 2021/8/27 9:52
 */
@Slf4j
@Api(tags = "购物车")
@RestController
@RequestMapping("/cart")
public class CartController {

    @Resource
    private RocketMqProducer rocketMqProducer;

    @ApiOperation(value = "购物车商品添加-同步添加")
    @PostMapping("/cartAdd")
    public Result<Void> cartItemAdd(@RequestBody CartItemEvent event) {
        for (int i = 1; i <= 1000; i++) {
            // 同步发送 - 在需要高可靠性的情况下，比如重要的通知消息或短信通知
            event.setItemId((long) i);
            event.setQuantity(1);
            SendResult sendResult = rocketMqProducer.syncSend(CartTopic.CART_ADD_TOPIC, event);
            // 异步发送 - 需要高吞吐量的情况下使用异步传输
           /* rocketMQTemplate.asyncSend(Topic.CART_ITEM_ADD_TOPIC , event, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    log.info("onSuccess:{}", sendResult.getSendStatus());
                }

                @Override
                public void onException(Throwable e) {
                    log.error("onException:", e);
                }
            });*/
        }
        return Result.ok();
    }

    @ApiOperation(value = "购物车商品添加-选择指定队列顺序消息")
    @PostMapping("/cartAddSelectQueueOrderly")
    public Result<Void> cartItemAddSelectQueueOrderly(@RequestBody CartItemEvent event) {
        for (int i = 0; i < 1000; i++) {
            event.setItemId((long) i);
            event.setQuantity(1);
            SendResult sendResult = rocketMqProducer.syncSendOrderly(CartTopic.CART_ADD_ORDERLY_TOPIC, event, "1024");
        }
        return Result.ok();
    }


    @ApiOperation(value = "购物车商品添加-事务消息")
    @PostMapping("/cartItemAddTransaction")
    public Result<Void> cartItemAddTransaction(@RequestBody CartItemEvent event) {
        // 发送事务消息
        event.setItemId((long) 1);
        event.setQuantity(1);
        rocketMqProducer.sendTransaction(CartTopic.CART_ADD_TOPIC, event, event);
        return Result.ok();
    }


    @ApiOperation(value = "购物车商品删除-异步发送")
    @PostMapping("/cartRemoved")
    public Result<Void> cartItemRemoved(@RequestBody CartItemEvent event) {
        // 同步发送
        event.setItemId((long) 1);
        event.setQuantity(1);
        rocketMqProducer.syncSend(CartTopic.CART_REMOVED_TOPIC, event);
        return Result.ok();
    }


    @ApiOperation(value = "购物车商品删除-延时消息")
    @PostMapping("/delayCartItemRemoved")
    public Result<Void> delayCartItemRemoved(@RequestBody CartItemEvent event) {
        event.setItemId((long) 1);
        event.setQuantity(1);
        rocketMqProducer.syncSendDelay(CartTopic.CART_REMOVED_TOPIC, event, 3000, 2);
        return Result.ok();
    }


}
